<?php 
error_reporting(E_ALL || E_STRICT);
//error_reporting(1792);

/**
 *
 */

class Main
{
    private $_gpcs;
    private $_log;
    private $_settings;
    private $_db;

    /** Modules **/
    public $_other; #TESTING MODULE

    private $_css;

    private $_debug;
    private $_pageInfo;

    private $_currentPage   = false;
    private $_currentMedia  = false;
    private $_currentModule = false;

    /**
     * @access public
     */
    public function __construct()
    {
//        require_once("classes/System/GPCSFilter.class.php");
//        $this->_gpcs     = new GPCSFilter();
//        require_once("classes/System/Log/LogFile.class.php");
//        $this->_log      = new LogFile('Main.log');
//
        
        $this->_settings = parse_ini_file('settings/mediadevil.ini');
        (bool) $this->_debug = $this->_settings['debug'];
        if(count($this->_settings) == 0) $this->_log->record('Empty ini file!', 'Main::__constructor');
        if(count($this->_settings) == 0)
        {
          $this->_log->record('mediadevil.ini file not present', __FILE__.' on line '.__LINE__);
          trigger_error("Can't load settings file", E_USER_ERROR);
        }
        else
        {
            try
            {
                $this->_db = new PDO('mysql:host='.$this->_settings['hostname'].';
                                            port=3306;
                                            dbname='.$this->_settings['database'],
                                            $this->_settings['username'],
                                            $this->_settings['password']);
                $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                if(!is_object($this->_db)) $this->_log->record($e->getMessage(), 'MySQL');
            }
            catch(PDOException $e)
            {
                //var_dump(get_class_methods($e));
                //var_dump($e);
                if($e->getMessage() === "SQLSTATE[42000] [1049] Unknown database '".
                												$this->_settings['database']."'")
                {
                    $this->_log->record('Unknown database type');
                    trigger_error("Unknown database", E_USER_ERROR);
                }

            }
        }
        require_once("lib/classes/PageInfo.class.php");
        $this->_pageInfo = new PageInfo($this->_settings['title']);
        $this->_css = array(0 => 'mediadevil', 
        					1 => 'debug');

        require_once("classes/Login/Login.class.php");
        //$this->_other    = new Login($this->_db);
        #TESTING ONLY
        //require_once("classes/Audio/AudioIndex.class.php");
        //$this->_module = new AudioIndex($this->_db, $this);
    }

    public function getPageInfo()
    {
        return $this->_pageInfo;
    }

    public function getCSS()
    {
        return $this->_css;
    }

    public function getCurrentPage()
    {
        if(!$this->_currentPage)
        {
            return 'Home';
        }   
        else return $this->_currentPage;
    }
    
    public function getModule($module, $media, $id)
    {
    	$module = ucfirst($module);
    	var_dump($module);
    	$a = require_once('classes/Modules/'.$module.'/'.$module.'.class.php');
    	$this->_currentModule = new $module($media, $id);
    	var_dump($a);
    	if(isset($id) && (!empty($id)))
    	{
    		echo '<pre>';
    		echo $this->_currentModule->getItem();
    	}
    	elseif(isset($media) && (!empty($media)))
    	{
    		echo '<pre>';
    		echo $this->_currentModule->getIndex();
    	}
    	else 
    	{
    		echo '<pre>';
    		echo 'Main::getModule("Module set but no media received")';
    	}
    }

    public function getDebug()
    {
        return $this->_debug;
    }

    public function getLog()
    {
        if(is_object($this->_log))
        {
            return $this->_log;
        }   
        else return false;

    }

    public function __toString()
    {
        return '';
    }

    /**
     * @access public
     */
    public function __destruct()
    {
    	echo "<pre>"; var_dump($this);
    }
}